home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue40 / Alfresco / AAVfyChk.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-11-01  |  2.5 KB  |  101 lines

  1. {*********************************************************}
  2. {* AAVfyChk                                              *}
  3. {* Copyright (c) Julian M Bucknall 1998                  *}
  4. {* All rights reserved.                                  *}
  5. {*********************************************************}
  6. {* Verifying credit card and ISBN check digits           *}
  7. {*********************************************************}
  8.  
  9. {Note: this unit is released as freeware. In other words, you are free
  10.        to use this unit in your own applications, however I retain all
  11.        copyright to the code. JMB}
  12.  
  13. unit AAVfyChk;
  14.  
  15. interface
  16.  
  17. function ValidateCreditCardNumber(const aValue : string) : boolean;
  18. function ValidateISBN(const aValue : string) : boolean;
  19.  
  20. implementation
  21.  
  22. function IsDigit(Ch : char) : boolean;
  23. begin
  24.   Result := Ch in ['0'..'9'];
  25. end;
  26.  
  27. function IsDigitOrX(Ch : char) : boolean;
  28. begin
  29.   Result := Ch in ['0'..'9', 'X'];
  30. end;
  31.  
  32. function ValidateCreditCardNumber(const aValue : string) : boolean;
  33. var
  34.   i     : integer;
  35.   Total : integer;
  36.   Dbl   : integer;
  37.   Ch    : char;
  38.   IsOddPosn : boolean;
  39. begin
  40.   if (aValue = '') then begin
  41.     Result := false;
  42.     Exit;
  43.   end;
  44.   Total := 0;
  45.   IsOddPosn := true;
  46.   for i := length(aValue) downto 1 do begin
  47.     Ch := aValue[i];
  48.     if IsDigit(Ch) then begin
  49.       if IsOddPosn then
  50.         inc(Total, ord(Ch) - ord('0'))
  51.       else begin
  52.         Dbl := (ord(Ch) - ord('0')) * 2;
  53.         inc(Total, Dbl);
  54.         if Dbl >= 10 then
  55.           dec(Total, 9);
  56.       end;
  57.       IsOddPosn := not IsOddPosn;
  58.     end;
  59.   end;
  60.   Result := (Total mod 10) = 0;
  61. end;
  62.  
  63. function ValidateISBN(const aValue : string) : boolean;
  64. var
  65.   i     : integer;
  66.   Total : integer;
  67.   Multiplier : integer;
  68.   FirstDigit : boolean;
  69.   Ch    : char;
  70. begin
  71.   if (aValue = '') then begin
  72.     Result := false;
  73.     Exit;
  74.   end;
  75.   FirstDigit := true;
  76.   for i := length(aValue) downto 1 do begin
  77.     Ch := aValue[i];
  78.     if FirstDigit then begin
  79.       if IsDigitOrX(Ch) then begin
  80.         if (Ch = 'X') then
  81.           Total := 10
  82.         else
  83.           Total := (ord(Ch) - ord('0'));
  84.         FirstDigit := false;
  85.         Multiplier := 2;
  86.       end
  87.     end
  88.     else {not the first digit} begin
  89.       if IsDigit(Ch) then begin
  90.         inc(Total, (ord(Ch) - ord('0')) * Multiplier);
  91.         inc(Multiplier);
  92.         if (Multiplier = 11) then
  93.           Multiplier := 1;
  94.       end;
  95.     end;
  96.   end;
  97.   Result := (Total mod 11) = 0;
  98. end;
  99.  
  100. end.
  101.